﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using MC.RideEventMVC.Models.Entity;

namespace MC.RideEventMVC.Models.Repository
{
    public class WayRepository : IWayRepository
    {
        private const int THE_LAST = 5;
        private RideEventDBEntities _context;

        public WayRepository()
        {
            _context = SingletonDbEntities.GetInstance();
        }

        public List<Way> GetLastWays()
        {
            var list = from w in _context.Way
                       where w.Event.DateBegin.Date >= DateTime.Now.Date
                       orderby w.Event.DateBegin.Date descending
                       select w;

            var result = list.Take(THE_LAST).ToList();

            return result;
        }

        public List<Way> GetLastWaysByEventId(int eventId)
        {
            var list = from w in _context.Way
                       where w.EventId == eventId
                       where w.Event.DateBegin.Date >= DateTime.Now.Date
                       orderby w.Event.DateBegin.Date descending
                       select w;

            var result = list.Take(THE_LAST).ToList();

            return result;
        }

        public List<Event> GetWaysByEventId(int eventId)
        {
            var list = from w in _context.Way
                       where w.EventId == eventId
                       where w.Event.DateBegin.Date >= DateTime.Now.Date
                       orderby w.Event.DateBegin.Date descending
                       select w;

            var result = list.ToList();

            return result;
        }
    }
}